package com.ymm.lulutong.controller;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperCargoDomainService;
import com.ymm.lulutong.repo.SystemDefinePriceServiceRepo;
import com.ymm.lulutong.request.CargoDefinePriceWebRequest;
import com.ymm.lulutong.utils.Result;
import com.ymm.system.api.dto.PutOnCargoRequest;
import com.ymm.system.api.dto.ShipperCargoBriefResponse;
import com.ymm.system.api.dto.ShipperCargoDetailResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.fileupload.disk.DiskFileItem;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/cargo")
public class ShipperCargoController {
    @Resource
    private ShipperCargoDomainService shipperCargoDomainService;

    @Resource
    private SystemDefinePriceServiceRepo systemDefinePriceServiceRepo;

    /**
     * 获取定价
     *
     * @param cargoPriceRequest 定价请求request
     * @param deliveryTime      前端传的时间字符串，格式为"yyyy-MM-dd HH:mm:ss"
     * @return 定价，单位（分）
     * @throws ParseException SimpleDateFormat异常
     */
    @PostMapping("/definePrice")
    public Result<Integer> definePrice(CargoDefinePriceWebRequest cargoPriceRequest, String deliveryTime) throws ParseException {
        if (cargoPriceRequest == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        if (Objects.isNull(deliveryTime) || deliveryTime.length() == 0){
            cargoPriceRequest.setCargoDeliveryTime(new Date());
        } else {
            SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            cargoPriceRequest.setCargoDeliveryTime(slf.parse(deliveryTime));
        }
        return systemDefinePriceServiceRepo.definePrice(cargoPriceRequest);
    }

    /**
     * 发货
     *
     * @param putOnCargoRequest 发货请求request
     * @param deliveryTime      前端传的时间字符串，格式为"yyyy-MM-dd HH:mm:ss"
     * @return 发货是否成功
     * @throws ParseException SimpleDateFormat异常
     */
    @PostMapping("/putOnCargo")
    public Result<ShipperCargoDetailResponse> putOnCargo(PutOnCargoRequest putOnCargoRequest, String deliveryTime) throws ParseException {
        System.out.println(putOnCargoRequest);
        if (putOnCargoRequest == null || deliveryTime == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        putOnCargoRequest.setCargoDeliveryTime(slf.parse(deliveryTime));
        try {
            Result<Long> putOnCargoResult = shipperCargoDomainService.putOnCargo(putOnCargoRequest, putOnCargoRequest.getShipperId());
            if (Objects.isNull(putOnCargoResult)) {
                return Result.fail(null, ResultCodeEnum.FAIL);
            }
            if (!putOnCargoResult.isSuccess()){
                return Result.fail(null, putOnCargoResult.getCode(), putOnCargoResult.getMessage());
            }
            Long cargoId = putOnCargoResult.getData();
            Result<ShipperCargoDetailResponse> cargo = shipperCargoDomainService.getCargoDetailByCargoId(cargoId);
            return Result.success(cargo.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    /**
     * 下架货源
     *
     * @param cargoId 货源Id
     * @return 下架是否成功
     */
    @PostMapping("/pullOffCargo")
    public Result<Long> pullOffCargo(Long cargoId, Long shipperId) {
        if (Objects.isNull(cargoId) || Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        try {
            return shipperCargoDomainService.pullOffCargo(cargoId, shipperId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    /**
     * 货源详细信息
     *
     * @param cargoId 货源Id
     * @return 货源详细信息
     */
    @GetMapping("/cargoDetail")
    public Result<ShipperCargoDetailResponse> getCargoDetail(Long cargoId) {
        if (Objects.isNull(cargoId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperCargoDomainService.getCargoDetailByCargoId(cargoId);
    }

    /**
     * 查询上架中的货源列表
     *
     * @param shipperId 货主Id
     * @return 上架中的货源列表
     */
    @GetMapping("/onShelfCargos")
    public Result<List<ShipperCargoBriefResponse>> getOnShelfCargos(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperCargoDomainService.getOnShelfCargosByShipperId(shipperId);
    }

    /**
     * 查询已下架的货源列表
     *
     * @param shipperId 货主Id
     * @return 已下架的货源列表
     */
    @GetMapping("/offShelfCargos")
    public Result<List<ShipperCargoBriefResponse>> getOffShelfCargos(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperCargoDomainService.getOffShelfCargosByShipperId(shipperId);
    }

    /**
     * 上传货源图片
     *
     * @param shipperId 货主Id
     * @param image     图片
     * @return 路径
     */
    @PostMapping("/upload/cargoPhoto")
    public Result<String> uploadCargoPhoto(Long shipperId, MultipartFile image) {
        if (Objects.isNull(shipperId) || Objects.isNull(image)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }

        return shipperCargoDomainService.uploadCargoPhoto(shipperId, image);
    }
}
